package com.coder4j.biz.service;

import com.coder4j.biz.entity.WechatLinkEntity;
import com.coder4j.biz.mapper.WechatLinkMapper;
import com.coder4j.biz.vo.WechatLinkQuery;
import com.coder4j.biz.vo.WechatLinkVO;
import com.coder4j.cache.GroupCache;
import com.coder4j.starter.tools.util.DateUtils;
import com.coder4j.starter.web.BaseServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
 * 微信链接分享
 * @author keller
 * @date 2024-12-12 10:43:05
 */
@Service
@Slf4j
public class WechatLinkService extends BaseServiceImpl<WechatLinkMapper,WechatLinkEntity,WechatLinkVO> {


    @Value("${upload.wechatImg}")
    String uploadImgUrl;

    @Value("${nginx.wechatImg}")
    String nginxImgUrl;

    @Resource
    private WechatLinkMapper mapper;

    @Override
    public WechatLinkVO entity2Vo(WechatLinkEntity entity) {
        WechatLinkVO vo = new WechatLinkVO();
        BeanUtils.copyProperties(entity,vo);
        if(entity.getGroupId() != null){
            vo.setGroupName(GroupCache.getGroupName(entity.getGroupId()));
        }
        return vo;
    }

    @Override
    public WechatLinkEntity vo2Entity(WechatLinkVO vo) {
        WechatLinkEntity entity = new WechatLinkEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public WechatLinkEntity praseEntity(String link) throws Exception{
        // 连接到URL并获取HTML文档
        Document document = Jsoup.connect(link).get();

        // 获取所有的meta标签
        Elements metaTags = document.select("meta");
        WechatLinkEntity entity = new WechatLinkEntity();

        // 遍历meta标签并打印其属性
        for (Element metaTag : metaTags) {
            String property = metaTag.attr("property");
            String content = metaTag.attr("content");
            System.out.println("property: " + property + ", Content: " + content);
            switch (property){
                case "og:title":{
                    entity.setTitle(content);
                }break;
                case "og:url":{
                    entity.setLink(content);
                }break;
                case "og:image":{
                    String nginxImg = this.downloadImage(content);
                    entity.setImg(nginxImg);
                }break;
                default:break;
            }
        }
        return entity;
    }

    private String downloadImage(String imgUrl) throws Exception {
        URL url = new URL(imgUrl);
        String fileName = DateUtils.getTimeMask()+".jpg";
        File destination = new File(uploadImgUrl + File.separator + fileName);
        Files.copy(url.openStream(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
        return nginxImgUrl + "/" + fileName;
    }
}
